<template>
  <div id="global-header">
    <a-row :warp="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="header-title">
            <img src="@/assets/logo.svg" style="height: 32px; height: 32px" alt="logo" />
            <a-typography-title style="font-size: 18px; margin: 0">
              智能云图库系统
            </a-typography-title>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          theme="light"
          mode="horizontal"
          v-model:selectedKeys="selectedKeys"
          v-model:openKeys="openKeys"
          :items="routerItems"
          :style="{ lineHeight: '64px' }"
          @click="menuClick"
        >
          <a-menu-item key="1">nav 1</a-menu-item>
          <a-menu-item key="2">nav 2</a-menu-item>
          <a-menu-item key="3">nav 3</a-menu-item>
        </a-menu>
      </a-col>
      <a-col flex="120px">
        <div v-if="useLoginUserStore.loginUser.id">
          <a-dropdown>
            <div class="header-right">
              <a-avatar :src="useLoginUserStore.loginUser.userAvatar"></a-avatar>
              <span>{{ useLoginUserStore.loginUser.userName ?? '无名' }}</span>
            </div>
            <template #overlay>
              <a-menu>
                <a-menu-item @click="doLogout">
                  <LogoutOutlined />
                  退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
        <a-button v-else type="primary">登录</a-button>
      </a-col>
    </a-row>
  </div>
</template>

<script setup lang="ts">
import { userLogoutUsingPost } from '@/api/userController'
import { useUserLoginUserStore } from '@/stores/loginUserStore.ts'
import { HomeOutlined, LogoutOutlined } from '@ant-design/icons-vue'
import { message, type MenuProps } from 'ant-design-vue'
import { h, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

const openKeys = ref<string[]>(['sub1'])
const routerItems = ref<MenuProps['items']>([
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '首页',
    title: '首页',
  },
  {
    key: '/about',
    label: '关于',
    title: '关于',
  },
])
const router = useRouter()
const useLoginUserStore = useUserLoginUserStore()

onMounted(() => {
  if (window.location.pathname.includes('login') || window.location.pathname.includes('register')) {
    console.log('登录或注册')
  } else {
    useLoginUserStore.getLoginUser()
  }
})
// 菜单 当前高亮
const selectedKeys = ref<string[]>([])
// 监听路由变化，切换高亮
router.afterEach((to, from, next) => {
  selectedKeys.value = [to.path]
})

// 路由跳转
const menuClick: MenuProps['onClick'] = ({ item, key, keyPath }) => {
  router.push({
    path: key as string,
  })
}
/**
 * 退出登录
 */
const doLogout = async () => {
  const res = await userLogoutUsingPost()
  if (res.data.code === 0) {
    useLoginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    router.push({
      path: '/user/login',
    })
  }
}
</script>

<style scoped>
#global-header {
}
#global-header .header-title {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 12.5rem;
  gap: 1rem;
  height: 100%;
}
.header-right {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
}
</style>
